#!/usr/bin/env python
# coding: utf-8

# from .xxx_element import xxx_element
from .tnxxx_port import *
from .tnxxx_ftp import *

class tnxxx_base():    
    def __init__(self):
        # xxx_element.__init__(self)
        self.__connect_dic = {}
        self.__pre_ip = None
        self.__pre_slot = None
        self.__pre_port = None
    
    """
    slot = 0        表示连接主oam
    slot = 其它     表示nejump到单板
    """    
    def connect(self, ip, slot, port, usr=None, pwd=None):
        if CLI_PORT == port:
            slot = 0
        if not self._parse_ip_port(ip, slot, port):
            return False
        if (ip,slot,port) not in self.__connect_dic:
            if port == FTP_PORT:
                if slot!=0:
                    print ("FTP slot != 0")
                    return False
                self.__connect_dic[(ip,slot,port)] = tnxxx_ftp()
            # elif port == SMARTBIT: #SMARTBIT = 16385
            #     if slot!=0:
            #         print ("Smartbit slot != 0")
            #         return False
            #     self.__connect_dic[(ip,slot,port)] = smb_base()                
            else:
                self.__connect_dic[(ip,slot,port)] = tnxxx_port()
        return self.__connect_dic[(ip,slot,port)].connect(ip, slot, port, usr, pwd)
    
    def disconnect(self, ip=None, slot=None, port=None):
        if CLI_PORT == port:
            slot = 0
        if not self._parse_ip_port(ip, slot, port):
            return False
        if ip==None and slot==None and port==None:
            for ip_slot_port in self.__connect_dic:
                self.__connect_dic[ip_slot_port].disconnect()
            self.__connect_dic.clear()
        else:
            if (ip,slot,port) not in self.__connect_dic:
                print ("Error : port doesn't connected")
                return False
            self.__connect_dic[(ip,slot,port)].disconnect()
            self.__connect_dic.pop((ip,slot,port))
        self.__pre_ip = None
        self.__pre_slot = None
        self.__pre_port = None        
        return True
    
    def send(self, cmd, rtn_flag='', ip=None, slot=None, port=None, timeout=50):
        connect = self._get_connect(ip, slot, port)
        if connect == None:
            print("===tnxxx_base connect failed===")
            return False        
        # rtn = connect.send(cmd, timeout)
        rtn = connect.send(cmd, 10)        
        if rtn == None:            
            print("===tnxxx_base send failed===")
        if len(rtn_flag) <= 0:
            return connect.check_default_rtn(cmd, rtn)
        return 0 <= rtn.find(rtn_flag)
    
    def send_only(self, cmd, ip=None, slot=None, port=None, timeout=50):
        connect = self._get_connect(ip, slot, port)
        if connect == None:
            return False
        # return connect.send_only(cmd, timeout)
        return connect.send_only(cmd, 10)
    
    def send_rtn_str(self, cmd, ip=None, slot=None, port=None, timeout=50):
        connect = self._get_connect(ip, slot, port)
        if connect == None:
            return ''
        
        return connect.send(cmd, 10)
    
    def _get_connect(self, ip, slot, port):
        if not self._parse_ip_port(ip, slot, port):        
            return None
        if self.__pre_ip==None or self.__pre_slot==None or self.__pre_port==None:
            return None
        if (self.__pre_ip,self.__pre_slot,self.__pre_port) not in self.__connect_dic:
            if not self.connect(ip, slot, port):
                return None
        return self.__connect_dic[(self.__pre_ip,self.__pre_slot,self.__pre_port)]
    
    def _parse_ip_port(self, ip, slot, port):
        if ip==None and slot==None and port==None:
            return True
        if type(ip)!=str or type(slot)!= int or type(port)!=int:
            print ('Error : param type')
            return False 
        self.__pre_ip = ip
        self.__pre_slot = slot
        self.__pre_port = port       
        return True

if __name__ == '__main__':
    case = tnxxx_base()
    print (case.connect('192.168.210.179', 6000))
    print (case.send('c t'))
    print (case.send('show interface'))
    print (case.send('show ipaddr'))
    print (case.send_only('show lgcard'))
    print (case.send_rtn_str('show \t'))
    print (case.send_rtn_str('show arp'))
    print (case.disconnect())
    print ("-"*30,"The END","-"*30)